﻿using System.Diagnostics;
using AntdUI;
using DataAcquisitionBLL;
using DataAcquisitionDAL;
using Microsoft.IdentityModel.Tokens;
using SqlSugar.Extensions;

namespace DataAcquisition
{
    public partial class DevicePointAddForm : BaseForm
    {
        #region 属性

        private DevicePoint devicePoint = new DevicePoint();

        /// <summary>
        /// 设备
        /// </summary>
        public DeviceModel? DeviceObject;

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            PointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            PointNameInput.Text = "";
            PointAddressInput.Text = "";
            RangeMinInput.Text = "";
            RangeMaxInput.Text = "";
            OutMinInput.Text = "";
            OutMaxInput.Text = "";
            AmendInput.Text = "";
            AlarmHightInput.Text = "";
            AlarmLowInput.Text = "";
            ErrorRangeInput.Text = "";
            UnitInput.Text = "";
            RegisterTypeSelect.Text = "";
            if (DeviceObject is not null)
            {
                string decrypt = Encryption.DesDecrypt(DeviceObject.SlaveAddress, Device.key);
                DeviceNumberInput.Text = DeviceObject.Number.ToString();
                DeviceNameInput.Text = DeviceObject.Name;
                DeviceAddressInput.Text = decrypt;
            }
        }
        
        #endregion

        #region 系统方法
        public DevicePointAddForm()
        {
            InitializeComponent();
            Header.Text = "添加设备点位";
        }

        private void DevicePointAddForm_Load(object sender, EventArgs e)
        {
            Reload();
            
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            DevicePointModel model = new DevicePointModel();
            try
            {
                model.Number = Convert.ToUInt32(PointNumberInput.Text);
            }
            catch (Exception)
            {
                model.Number = 0;
            }
            model.Name = PointNameInput.Text;
            try
            {
                model.Address = Convert.ToUInt32(PointAddressInput.Text).ToString();
            }
            catch (Exception)
            {
                model.Address = "0";
            }
            model.PointType = RegisterTypeSelect.Text;
            try
            {
                model.RangeMin = Convert.ToDouble(RangeMinInput.Text);
            }
            catch (Exception)
            {
                model.RangeMin = 0;
            }
            try
            {
                model.RangeMax = Convert.ToDouble(RangeMaxInput.Text);
            }
            catch (Exception)
            {
                model.RangeMax = 0;
            }
            try
            {
                model.OutMin = Convert.ToDouble(OutMinInput.Text);
            }
            catch (Exception)
            {
                model.OutMin = 0;
            }
            try
            {
                model.OutMax = Convert.ToDouble(OutMaxInput.Text);
            }
            catch (Exception)
            {
                model.OutMax = 0;
            }
            try
            {
                model.AlarmMin = Convert.ToDouble(AlarmLowInput.Text);
            }
            catch (Exception)
            {
                model.AlarmMin = 0;
            }
            try
            {
                model.AlarmMax = Convert.ToDouble(AlarmHightInput.Text);
            }
            catch (Exception)
            {
                model.AlarmMax = 0;
            }
            try
            {
                model.ErrorRange = Convert.ToDouble(ErrorRangeInput.Text);
            }
            catch (Exception)
            {
                model.ErrorRange = 0;
            }
            try
            {
                model.Amend = Convert.ToDouble(AmendInput.Text);
            }
            catch (Exception)
            {
                model.Amend = 0;
            }
            model.Unit = UnitInput.Text;
            try
            {
                model.DeviceNumber = Convert.ToUInt32(DeviceNumberInput.Text);
            }
            catch (Exception)
            {
                model.DeviceNumber = 0;
            }
            model.DeviceName = DeviceNameInput.Text;
            if (DeviceObject is not null)
            {
                model.DeviceAddress = DeviceObject.SlaveAddress;
            }
            else
            {
                model.DeviceAddress = string.Empty;
            }

            Response response = devicePoint.AddDevicePoint(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                Reload();
                this.Close();
                if (this.Owner is not null)
                {
                    DevicePointManageForm frm = (DevicePointManageForm)this.Owner;
                    frm.LoadList();
                }
            }
        }

        private void RegisterTypeSelect_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            if (RegisterTypeSelect.Text == "Input" || RegisterTypeSelect.Text == "Holding")
            {
                GroupBox3.Enabled = true;
                GroupBox3.Visible = true;
            }
            else
            {
                GroupBox3.Enabled = false;
                GroupBox3.Visible = false;
            }
        }

        #endregion
    }
}
